<template>
	<view class="menu-wrapper">

		<u-row gutter="20">
			<u-col span="6" v-if="Span == '6'">
				<HButton plain @click="onMoreButton">更多操作</HButton>
			</u-col>
			<u-col :span="Span">
				<HButton @click="onButton(HandlerButton.handlerButton)">{{HandlerButton.handlerButton.text}}</HButton>
			</u-col>
			<!-- 转工单 -->
			<u-popup mode="bottom" v-model="visibleToOrder" :closeable="true" borderRadius="20" height="90%">
				<returnOrder :itemCode="ItemCode" :data="dataPackage"></returnOrder>
			</u-popup>
			<!-- 终止 -->
			<u-popup mode="bottom" v-model="visibleEnd" :closeable="true" borderRadius="20">
				<stop :itemCode="ItemCode"></stop>
			</u-popup>
		</u-row>
	</view>


</template>

<script>
	const PermissionsClass = {
		/**
		 * 需求创建人
		 * */
		DemandCreateUser: 'DemandCreateUser',
		/**
		 * 调研负责人
		 * */
		InvestigatePrincipal: 'InvestigatePrincipal',
		/**
		 * 调研参与人
		 * */
		InvestigateUser: 'InvestigateUser',
		/**
		 * 项目负责人
		 * */
		ProjectPrincipal: 'ProjectPrincipal',
		/**
		 *  项目创建人
		 * */
		ProjectCreateUser: 'ProjectCreateUser',
		/**
		 * 项目审核人
		 * */
		ProjectReviewUser: 'ProjectReviewUser',
		/**
		 * 项目参与人
		 * */
		ProjectUsers: 'ProjectUsers',
		/**
		 * 任务负责人
		 * */
		TaskPrincipal: 'TaskPrincipal',
		/**
		 * 任务成员
		 * */
		TaskMember: 'TaskMember',
		/**
		 * 任务负责人 +任务成员
		 * */
		TaskPrincipalOrMember: 'TaskPrincipalOrMember',
		/**
		 * 所有人
		 * */
		AllUsers: 'AllUsers',
		/**
		 * ITBP 成员
		 * */
		ITBP: 'ITBP',
		/**
		 * 操作人
		 */
		OperateUser: 'OperateUser',
		/**
		 * 分配调研
		 */
		Allocation: 'Allocation',
		/**
		 * 需求评审
		 */
		DemandReview: 'DemandReview',
		/**
		 * 初定级
		 */
		PreliminaryLevel: 'PreliminaryLevel',
		/**
		 * 技术分析人
		 */
		TechnicalAnalysis: 'TechnicalAnalysis',
		/**
		 * 上一步操作人
		 */
		BeforeHandlerUser: 'BeforeHandlerUser',
	};

	import HButton from '@/components/button/index.vue'
	import { Storage_UserInfo, Storage_Permission } from '@/common/index.js'

	import PagesCourseRouterConfig from '@/pagesCourse/routerConfig.js'
	import PagesProjectRouterConfig from '@/pagesProject/routerConfig.js'
	import PagesDemandRouterConfig from '@/pagesDemand/routerConfig.js'
	import returnOrder from '@/pages/demand/operation/return.vue'
	import stop from '@/pages/demand/operation/stop.vue'
	export default {

		components: {
			HButton,
			returnOrder,
			stop
		},

		props: {

			data: {
				type: Object,
				default: () => {
					return {}
				}
			},

			state: {
				type: String,
				default: ''
			},

			dataPackage: {
				type: Object,
				default: () => {
					return {}
				}
			},
		},

		computed: {
			ItemCode() {
				console.log(this.data.itemCode, 'this.data.itemCode');
				return this.data.itemCode
			},
			MyPermissions(v) {
				return Storage_Permission()
			},
			// 是否有调研分配权限
			IsAllocation() {
				let permissions = uni.getStorageSync('permissions')
				if (permissions?.length) {
					return permissions.includes('DEMAND_ALLOCATION')
				} else {
					return false
				}
			},
			State() {
				return this.data.showState;
			},
			Data() {
				return this.data;
			},
			MyId: (v) => {
				let { id } = Storage_UserInfo()
				console.log(id, 'id-------');
				return id
			},
			Users() {
				const data = this.Data;
				const createUser = data.createUser;
				const principal = data.principal;
				// 调研成员
				const uers = data.users || '';
				// 技术分析成员
				const technicalAnalysisUsers = data.analystTechnical || '';
				return {
					uers,
					createUser,
					principal,
					technicalAnalysisUsers,
				};
			},
			IsCreateUser() {
				return this.Users.createUser == this.MyId ? PermissionsClass.DemandCreateUser : '';
			},
			// 调研成员
			IsUsers() {
				if (this.Users.uers.includes(String(this.MyId))) {
					return PermissionsClass.InvestigateUser;
				}
				return '';
			},
			// 调研负责人
			IsPrincipal() {
				return this.Users.principal == this.MyId ? PermissionsClass.InvestigatePrincipal : '';
			},
			// 是否有审核权限
			IsITBP() {
				if (this.MyPermissions.includes('ITBP_MANAGER')) {
					return PermissionsClass.ITBP;
				}
				return '';
			},
			// 是否有调研分配权限
			IsAllocation() {
				if (this.MyPermissions.includes('DEMAND_ALLOCATION')) {
					return PermissionsClass.Allocation;
				}
				return '';
			},
			// 是否有需求评审权限
			IsDemandReview() {
				if (this.MyPermissions.includes('DEMAND_REVIEW')) {
					return PermissionsClass.DemandReview;
				}
				return '';
			},
			// 是否是上一步的操作人
			IsBeforeHandlerUser() {
				return this.Users.handlerUser == this.MyId ? PermissionsClass.BeforeHandlerUser : '';
			},
			// 技术分析人
			IsTechnicalAnalysis() {
				return this.Users.technicalAnalysisUsers.includes(this.MyId) ? PermissionsClass.TechnicalAnalysis : '';
			},
			RenderItem() {
				const end = { text: '终止', code: 'end', permissionsCode: [PermissionsClass.DemandCreateUser, PermissionsClass.ITBP], };
				const toOrder = { text: '转工单', code: 'toOrder', permissionsCode: [PermissionsClass.DemandCreateUser, PermissionsClass.ITBP], };

				const assign = { text: '分配', router: PagesDemandRouterConfig.Distribute, code: 'distribute', permissionsCode: [PermissionsClass.Allocation], };
				const review = { text: '评审', router: PagesDemandRouterConfig.DemandReview, code: 'review', dataCode: this.State, permissionsCode: [PermissionsClass.InvestigatePrincipal, PermissionsClass.ITBP], };
				const research = { text: '调研', router: PagesDemandRouterConfig.Survey, code: 'research', permissionsCode: [PermissionsClass.InvestigatePrincipal, PermissionsClass.InvestigateUser, PermissionsClass.ITBP, PermissionsClass.DemandCreateUser], };
				const demandAnalysis = { text: '需求分析', router: PagesDemandRouterConfig.DemandAnalysis, code: 'demandAnalysis', permissionsCode: [PermissionsClass.InvestigatePrincipal, PermissionsClass.InvestigateUser, PermissionsClass.ITBP, PermissionsClass.DemandCreateUser], };
				const technicalAnalysis = { text: '技术分析', router: PagesDemandRouterConfig.TechnicalAnalysis, code: 'technicalAnalysis', permissionsCode: [PermissionsClass.ITBP, PermissionsClass.DemandCreateUser, PermissionsClass.TechnicalAnalysis], };
				const demandAssess = { text: '需求评估', router: PagesDemandRouterConfig.DemandAssess, code: 'demandAssess', permissionsCode: [PermissionsClass.InvestigatePrincipal, PermissionsClass.InvestigateUser, PermissionsClass.ITBP, PermissionsClass.DemandCreateUser], };
				const demandReview = { text: '需求评审', router: PagesDemandRouterConfig.DemandReview, code: 'demandReview', permissionsCode: [PermissionsClass.DemandReview], };
				// 初定级
				const preliminaryLevel = { text: '初定级', router: PagesDemandRouterConfig.PreliminaryLevel, code: 'preliminaryLevel', permissionsCode: [PermissionsClass.ITBP, PermissionsClass.DemandCreateUser], };

				let arr = [];
				switch (this.state) {
					case 'demand_init':
						arr = [assign, end, toOrder];
						break;
					case 'demand_inv_review':
						arr = [demandReview, end, toOrder];
						break;
						// 初定级
					case 'WAIT_GRADING':
						arr = [preliminaryLevel, end, toOrder];
						break;
						// 待需求分析
					case 'demand_analysis':
						arr = [demandAnalysis, end, toOrder];
						break;
						// 待技术分析
					case 'technical_analysis':
						arr = [technicalAnalysis, end, toOrder];
						break;
						// 待需求评估
					case 'demand_assess':
						arr = [demandAssess, end];
						break;
					case 'demand_inv':
						arr = [research, end, toOrder];
						break;
					case 'demand_inv_review_next':
						arr = [research, end, toOrder];
						break;
					case 'item_establish':
						arr = [end];
						break;
					case 'item_program_review':
						arr = [review];
						if (this.IsPrincipal) {
							arr.push(end, toOrder);
						} else {
							arr.push(end, toOrder);
						}
						break;
					case 'implement_start':
						arr = [end];
						break;
					case 'program_review_reject':
						arr = [end, toOrder];
						break;
					case 'end_inv_reject':
						arr = [end];
						break;
					case 'end_project_order':
						arr = [end];
						break;
					case 'end_assess_reject':
						arr = [end];
						break;
					default:
						arr = [];
				}
				return arr;
			},
			HandlerButton() {
				let renderPermissionsButtons = [];

				for (const i of this.RenderItem) {
					let val = null;
					if (i.permissionsCode.includes(this.IsITBP)) {
						val = i;
					} else if (i.permissionsCode.includes(this.IsPrincipal)) {
						val = i;
					} else if (i.permissionsCode.includes(this.IsCreateUser)) {
						val = i;
					} else if (i.permissionsCode.includes(this.IsITEM_AUDIT)) {
						val = i;
					} else if (i.permissionsCode.includes(this.IsUsers)) {
						val = i;
					} else if (i.permissionsCode.includes(this.IsAllocation)) {
						val = i;
					} else if (i.permissionsCode.includes(this.IsDemandReview)) {
						val = i;
					} else if (i.permissionsCode.includes(this.IsBeforeHandlerUser)) {
						val = i;
					} else if (i.permissionsCode.includes(this.IsTechnicalAnalysis)) {
						val = i;
					}
					if (val) {
						renderPermissionsButtons.push(val);
					}
				}
				let len = renderPermissionsButtons.length
				let moreButton = []
				let handlerButton = []
				if (len > 0) {

					handlerButton = renderPermissionsButtons[0]
					moreButton = renderPermissionsButtons.slice(1)



				}
				let obj = {
					moreButton,
					handlerButton

				}
				return obj
			},
			Span() {
				if (this.HandlerButton.moreButton.length) {
					return '6'
				}
				return '12'

			}
		},
		data() {
			return {
				visibleToOrder: false,
				visibleEnd: false
			}
		},
		methods: {
			onMoreButton() {
				let itemList = this.HandlerButton.moreButton.map((item) => item.text)
				let itemListCode = this.HandlerButton.moreButton.map((item) => item.code)
				let self = this
				wx.showActionSheet({
					itemList: itemList,
					success({ tapIndex }) {
						let code = itemListCode[tapIndex]
						if (code == 'end') {
							self.visibleEnd = true
						} else {
							self.visibleToOrder = true
						}

					},
				})
			},
			onButton(params) {
				let { router } = params
				let value = {
					itemCode: this.ItemCode
				}
				let routerParams = this.$u.queryParams(value)
				wx.navigateTo({
					url: `${router}${routerParams}`
				})

			}

		}
	}
</script>

<style lang="scss" scoped>
	.menu-wrapper {
		padding: 18rpx 0;
	}
</style>
